﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static WinFormsApp1.SuYi.Week2WinForm.WaitingWinFormShow;

namespace WinFormsApp1.SuYi.Week2WinForm
{
    public partial class DeductionWinFormShwo : Form
    {
        public DeductionWinFormShwo()
        {
            InitializeComponent();
            comboBox1.Items.Add("启用");
            comboBox1.Items.Add("禁用");
        }


        public int PageIndex = 1;
        public int PageSize = 3;
        public int totalPage = 0;
        public int totalCount = 0;
        public bool DeductionStatu = false;
        public string DeductionName = "";
        private void DeductionWinFormShwo_Load(object sender, EventArgs e)
        {
            GetDeduction();
        }
        /// <summary>
        /// 显示
        /// </summary>
        public async void GetDeduction()
        {
            var page = new PageDto()
            {
                PageIndex = PageIndex,
                PageSize = PageSize,
                DeductionStatu = DeductionStatu,
                DeductionName = DeductionName,
            };

            var list = await HttpClientHelper.RequestUrl<PageDto, ApiResultFY2<List<DeductionsDto>>>(URLCommon.Url2, "/Week2Get/QueryDeductions", HttpClientHelper.MethodType.POST, page);

            this.dataGridView1.DataSource = list.Data.list;
            totalCount = list.Data.totalCount;
            totalPage = list.Data.totalPage;
        }

        /// <summary>
        /// 分页
        /// </summary>
        public class PageDto
        {
            /// <summary>
            /// 页码
            /// </summary>
            public int PageIndex { get; set; }
            /// <summary>
            /// 条数
            /// </summary>
            public int PageSize { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool DeductionStatu { get; set; }
            /// <summary>
            /// 扣减项名称
            /// </summary>
            public string DeductionName { get; set; }
        }

        /// <summary>
        /// 扣减项设置显示
        /// </summary>
        public class DeductionsDto
        {
            /// <summary>
            /// 扣减项编号
            /// </summary>
            [Key]
            public int DeductionId { get; set; }
            /// <summary>
            /// 扣减项名称
            /// </summary>
            public string DeductionName { get; set; }
            /// <summary>
            /// 扣减项金额
            /// </summary>
            public decimal DeductionPrice { get; set; }
            /// <summary>
            /// 项目所属业务
            /// </summary>
            public string DeductionBusiness { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool DeductionStatu { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string DeductionRemark { get; set; }
        }

        /// <summary>
        /// 查询按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DeductionName = this.textBox1.Text;
            DeductionStatu = comboBox1.Text == "启用" ? true : false;
            GetDeduction();
        }

        /// <summary>
        /// 状态显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["使用状态"].Index && e.Value != null)
            {
                if (e.Value.ToString() == "True")
                {
                    e.Value = "启用";
                    e.FormattingApplied = true;
                }
                else
                {
                    e.Value = "禁用";
                    e.FormattingApplied = false;
                }
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            GetDeduction();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton4_Click(object sender, EventArgs e)
        {
            PageIndex = totalPage;
            GetDeduction();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex--;
            }
            else
            {
                MessageBox.Show("已经是第一页了");
                return;
            }
            GetDeduction();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click(object sender, EventArgs e)
        {
            if (PageIndex < totalPage)
            {
                PageIndex++;
            }
            else
            {
                MessageBox.Show("已经是最后一页了");
                return;
            }
            GetDeduction();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["删除"].Index)
            {
                var waitdel = (int)e.RowIndex;
                if (waitdel > 0)
                {
                    int DeductionId = Convert.ToInt32(dataGridView1.Rows[waitdel].Cells["扣减项编号"].Value);

                    DeleteIds deleteId = new DeleteIds()
                    {
                        DeductionId = DeductionId
                    };

                    var item = await HttpClientHelper.RequestUrl<DeleteIds, ApiResult<List<DeleteIds>>>(URLCommon.Url1, "/Week2Setup/DeleteDeductions", HttpClientHelper.MethodType.POST, deleteId);



                    if (item.Code == 1)
                    {
                        MessageBox.Show("删除成功");
                        GetDeduction();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                }
            }
        }


        public class DeleteIds
        {
            public int DeductionId { get; set; }
        }
    }
}
